﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartyManagement.Models;

namespace PartyManagement.Controllers
{
    public class MenuController : Controller
    {
        private PartyManagementContext db = new PartyManagementContext();

        //
        // GET: /Menu/

        public ActionResult Index()
        {
            int userId = Convert.ToInt32(Session["userID"]);

            if(userId==0) 
                return RedirectToAction("Login","Account");
            else 
            return View(db.Menus.ToList());
        }
        public ActionResult IndexWithEvent()
        {
            var menus = db.Menus.Include(m => m.Event);

            return View(menus.ToList());
        }

        //
        // GET: /Menu/Details/5

        public ActionResult Details(int id = 0)
        {

            Menu menu = db.Menus.Find(id);

            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }

        public ActionResult DetailsWithEvent(int eventID = 0)
        {

            Menu menu = db.Menus.FirstOrDefault(p => p.eventID == eventID);

            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }
        //
        // GET: /Menu/Create

        [HttpGet]
        public ActionResult Create(int id, string eventName)
        {
            Menu menu = db.Menus.FirstOrDefault(p => p.eventID == id);

            ViewBag.eventName = eventName;
            ViewBag.eventID = id;

            if (menu != null)
            {

                return RedirectToAction("Index", "BookEvent");
            }

            return View();
        }

        //
        // POST: /Menu/Create

        [HttpPost]
        public ActionResult Create(Menu menu, int eventID)
        {
            Menu menutest = db.Menus.FirstOrDefault(p => p.eventID == eventID);
            Event even = db.Events.FirstOrDefault(p => p.eventID == eventID);

            if (ModelState.IsValid)
            {
                if (menutest == null)
                {

                    db.Menus.Add(menu);
                    even.hasMenu = true;
                    db.SaveChanges();
                    return RedirectToAction("Index", "BookEvent");
                }

            }

            //ViewBag.eventID = eventID;
            return View(menu);
        }

        //
        // GET: /Menu/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Menu menu = db.Menus.Find(id);

            if (menu == null)
            {
                return HttpNotFound();
            }
            ViewBag.eventID = menu.eventID;
            ViewBag.eventName = menu.Event.eventName;
            return View(menu);
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        public ActionResult Edit(Menu menu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(menu).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.eventID = new SelectList(db.Events, "eventID", "eventName", menu.eventID);
            return View(menu);
        }

        //
        // GET: /Menu/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Menu menu = db.Menus.Find(id);
            if (menu == null)
            {
                return HttpNotFound();
            }
            return View(menu);
        }

        //
        // POST: /Menu/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Menu menu = db.Menus.Find(id);
            db.Menus.Remove(menu);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //GET /GetMenuForEvent/id

        public ActionResult GetMenuForEvent(int eventID)
        {
            Session["event_id"] = null;
            Session["event_id"] = eventID;
            Event eve = db.Events.Single(p => p.eventID == eventID);
            if (eve == null)
            {
                return View("Error", "Event doesn't exist!");
            }
            Menu menu = db.Menus.Single(p => p.eventID == eve.eventID);
            List<FoodInMenu> foodInMenus = db.FoodInMenus.Where(p => p.menuID == menu.menuID).ToList();

            return View(foodInMenus);
        }

        [HttpPost]
        public ActionResult GetMenuForEvent(int[] foodId, int[] quantity)
        {
            int i = 0;
            foreach (var itemId in foodId)
            {
                FoodInMenu foodInMenu = db.FoodInMenus.Find(itemId);
                int qtyBook = foodInMenu.quantity;
                foodInMenu.quantity = quantity[i];

                Food food = db.Foods.SingleOrDefault(p => p.foodID == foodInMenu.foodID);
                int qty = food.quantity;
                if ((qty - quantity[i] + qtyBook) < 0)
                {
                    return View("Error", String.Format("Quantity of {0} isn't enough!", food.foodName));
                }
                else
                {
                    food.quantity = food.quantity - quantity[i] + qtyBook;
                }

                i++;
            }
            db.SaveChanges();
            return RedirectToAction("GetMenuForEvent", "Menu", new { eventID = Session["event_id"] });
        }

        public ActionResult DeleteFromMenu(int id)
        {
            FoodInMenu foodInMenu = db.FoodInMenus.Find(id);
            if (foodInMenu == null)
            {
                return View("Error", "Food/juice doesn't exist!");
            }
            Food food = db.Foods.SingleOrDefault(p => p.foodID == foodInMenu.foodID);
            food.quantity += foodInMenu.quantity;
            db.FoodInMenus.Remove(foodInMenu);
            db.SaveChanges();
            return RedirectToAction("GetMenuForEvent", "Menu", new { eventID = Session["event_id"] });
        }
    }
}